<template>
  <div class="m-login">
    <div class="content">
      <h1>管理后台</h1>
      <el-alert v-show="errorMsg"
                v-bind:title="errorMsg"
                @close="closeAlert"
                type="error">
      </el-alert>

      <el-form class="login-form">
        <el-form-item>
          <el-input
            type="text"
            placeholder="请输入账号"
            icon="user"
            v-model.trim="name"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            type="password"
            placeholder="请输入密码"
            icon="user"
            autocomplete='off'
            v-model.trim="password"
            @keyup.enter="check"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="check">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import md5 from 'blueimp-md5'
  import Vue from 'vue'

  export default {
    data() {
      return {
        showError: false,
        errorMsg: '',
        name: '',
        password: '',
        ifRequesting: false
      }
    },
    methods: {
      async getAdminInfo() {
        const { code, data } = await this.$api.adminCurrentInfo()
        if (code === 200) {
          this.$store.commit('updateUserValue', { key: 'username', value: data.username })
        }
      },
      async login(aLoginName, aPassword) {
        this.ifRequesting = true
        const { code, data } = await this.$api.adminLogin({
          aLoginName,
          aPassword
        })
        this.ifRequesting = false
        if (code === 0) {
          // 修改 vuex 的用户数据
          this.$store.commit('updateLoginStatus', true)
          this.$store.commit('updateSessionId', data.session_id)
          await this.getUserInfo()
        } else {
          this.errorMsg = data
        }
      },

      async getUserInfo() {
        const { code, data } = await this.$api.userGetInfo()
        if (code === 0) {
          Vue.prototype.aFirmId = data.list[0].id
          this.$store.commit('updateLoginStatus', true)
          this.$store.commit('updateUserData', data)
          this.$router.replace({ name: 'main' })
        }
      },

      async check() {
        if (this.ifRequesting) return false
        const name = this.name
        const password = this.password
        if (!name) {
          this.errorMsg = '请填写账号!'
        } else if (this.password.length < 6) {
          this.errorMsg = '密码不少于6位!'
        } else {
          this.errorMsg = ''
          this.login(name, md5(password))
        }
      },
      closeAlert() {
        this.errorMsg = ''
      }
    }
  }
</script>

<style lang="less">
  .m-login {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    // background: url("../assets/login-bg.png") no-repeat center;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;

    .content {
      width: 350px;
      margin-top: -250px;
      text-align: center;
      color: white;

      h1 {
        font-weight: normal;
        margin-bottom: 20px;
        font-size: 36px;
        color: @t-primary;
      }

      .el-alert {
        margin-bottom: 28px;
      }

      .login-form {
        padding: 40px 30px;
        background: rgba(255,255,255,0.30);
        box-shadow: 0px 5px 4px 1px rgba(113,116,121,0.37);
        border-radius: 10px;

        .el-input__icon {
          left: 0;
          right: auto;
        }
        .el-input__icon+.el-input__inner {
          padding-left: 35px;
          padding-right: 0;
        }
        .el-icon-user:after {
          width: 18px;
          height: 20px;
          background: url("../../assets/icon/account.png");
          background-size: 100% 100%;
        }
        .el-icon-password:after {
          width: 15px;
          height: 20px;
          background: url("../../assets/icon/password.png");
          background-size: 100% 100%;
        }

        .el-input:nth-child(1) {
        }
        .el-button {
          width: 100%;
        }
      }
    }
  }
</style>
